# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        l=1
        r=n
        while l<r:
            a=int(l+(r-l)/2)
            if isBadVersion(a):
                r=a
            else:
                l=a+1
        return l